package com.zygh.dlqwservice.module.service.impl;

import com.zygh.dlqwservice.common.en.EmployeeStatusEnum;
import com.zygh.dlqwservice.common.en.JudgeEnum;
import com.zygh.dlqwservice.config.token.RequestTokenBO;
import com.zygh.dlqwservice.module.entity.SysUser;
import com.zygh.dlqwservice.module.mapper.SysUserMapper;
import com.zygh.dlqwservice.module.service.ISysUserService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;

/**
 * [  ]
 *
 * @date
 * @since JDK1.8
 */

@Service
public class LoginTokenService {

    private static final Logger log = LoggerFactory.getLogger(LoginTokenService.class);

    /**
     * 过期时间一天
     */
    private static final int EXPIRE_SECONDS = 1 * 24 * 3600;
    /**
     * jwt加密字段
     */
    private static final String CLAIM_ID_KEY = "user_id";

    //@Value("${jwt}")
    private String jwtKey = "dlqw-admin-jwt-key";

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 功能描述: 生成JWT TOKEN
     *
     * @param sysUser
     * @return
     */
    public String generateToken(SysUser sysUser) {
        int id = sysUser.getUserId();
        /**将token设置为jwt格式*/
        String baseToken = UUID.randomUUID().toString();
        LocalDateTime localDateTimeNow = LocalDateTime.now();
        LocalDateTime localDateTimeExpire = localDateTimeNow.plusSeconds(EXPIRE_SECONDS);
        Date from = Date.from(localDateTimeNow.atZone(ZoneId.systemDefault()).toInstant());
        Date expire = Date.from(localDateTimeExpire.atZone(ZoneId.systemDefault()).toInstant());
        Claims jwtClaims = Jwts.claims().setSubject(baseToken);
        jwtClaims.put(CLAIM_ID_KEY, id);
        String compactJws = Jwts.builder().setClaims(jwtClaims).setNotBefore(from).setExpiration(expire).signWith(SignatureAlgorithm.HS512, jwtKey).compact();
        //登录人员信息存放
        SysUser sysUser1 = sysUserMapper.selectById(id);
        System.out.println(sysUser1);
        return compactJws;
    }

    /**
     * 功能描述: 根据登陆token获取登陆信息
     *
     * @param
     * @return
     */
    public RequestTokenBO getEmployeeTokenInfo(String token) {
        Long userId = -1L;
        try {
            Claims claims = Jwts.parser().setSigningKey(jwtKey).parseClaimsJws(token).getBody();
            String idStr = claims.get(CLAIM_ID_KEY).toString();
            userId = Long.valueOf(idStr);
        } catch (Exception e) {
            log.error("getEmployeeTokenInfo error:{}", e);
            return null;
        }
        System.out.println(sysUserMapper);
        SysUser sysUser = sysUserMapper.selectById(userId);
        if (sysUser == null) {
            return null;
        }

        if (EmployeeStatusEnum.DISABLED.getValue().equals(sysUser.getIsDelete())) {
            return null;
        }


        if (JudgeEnum.YES.equals(sysUser.getIsDelete())) {
            return null;
        }

        return new RequestTokenBO(sysUser);
    }



}
